var app = getApp();
var _id = '';
Page({
    data: {
        title:'',
        total: '',
        tel:'',
        description:'',
        submitStatus: false,
        templs:[],
        unit:'',
        isopen:"true"
    },
    insert: function () {
      this.data.unit='';
      var cb = this.data.templs;
      var index = this.data.templs.length;
      var templdata = {};
      templdata["name" ]='';
      templdata["switch" ] = true;
      templdata["type"] = '0';
      templdata["typelist"] = [{ 'key': '文字', 'value': 'text' }, { 'key': '数值', 'value': 'digit' }, { 'key': '列表', 'value': 'list' }, { 'key': '日期', 'value': 'date' }];
      templdata["typevalue"] ="选项1,选项2";
      
      cb.push(templdata);
      for (var e in cb) {
        if (index == e && !cb[e]['flag']) {
          cb[e]['flag'] = 1
        } else {
          cb[e]['flag'] = 0;
        }
      }

      this.setData({
        templs: cb
      });
    },
    delete: function (e) {
      var _this = this;
      wx.showModal({
        title: '请确认',
        content: '你确定要删除该数据项吗？',
        success: function (res) {
          _this.data.unit = '';
          var cb = _this.data.templs;
          cb.splice(e.target.dataset.id, 1);
          _this.setData({
            templs: cb
          });
        }
      });


    }, 
    bindext: function (e) {
       var index= e.target.dataset.id;
       for (var e in this.data.templs) {
         if (index == e && !this.data.templs[e]['flag']) {
           this.data.templs[e]['flag'] = 1
         }else{
           this.data.templs[e]['flag'] = 0;
         }
       }
       this.setData({
         templs: this.data.templs
       });

    }, 

    bindPickerChange: function (e) {
      this.data.templs[e.target.dataset.id].type = e.detail.value;
      this.setData({
        templs: this.data.templs
      });
    },
    bindNamechange: function (e) {
      this.data.templs[e.target.dataset.id].name=e.detail.value;
      this.setData({
        templs: this.data.templs
      });
    }, 
    bindTpyeValuChange: function (e) {
      this.data.templs[e.target.dataset.id].typevalue = e.detail.value;
      this.setData({
        templs: this.data.templs
      });
    }, 
    bingSwitchChange: function (e) {
      this.data.templs[e.target.dataset.id].switch = e.detail.value;
      this.setData({
        templs: this.data.templs
      });
    }, 
    switch1Change: function (e) {
      this.setData({
        isopen: e.detail.value
      });
    },


    bindStartTimeChange: function(e) {
        this.setData({
            startTime: e.detail.value
        });
    },
    onShow: function(option) {
        wx.setNavigationBarTitle({
            title: '创建填报'
        });
    },
    bindEndTimeChange: function(e) {
        this.setData({
            endTime: e.detail.value
        });
    },
    goToBack: function() {
        wx.navigateBack({
            delta: 1
        });
    },
    number: function(e){

    },
    formSubmit: function(e) {
      console.info(this.data);
      console.info(this.data.id);
        var _this = this;
        var submitObject = e.detail.value || {};
        var status = true;
        var message = "数据填写不完整";
        for (var key in submitObject) {
            console.log(key + '=' + submitObject[key]);
            if (!submitObject[key]) {
              if (key =="title"){ message ="主题";}
              if (key == "startTime") { message = "开始时间"; }
              if (key == "endTime") { message = "结束时间"; }
              if (key == "total") { message = "人数"; }
              if (key == "tel") { message = "联系电话"; }
              if (key == "description") { message = "描述"; }
                // status = false;
              wx.showToast({
                title: message+"必填",
                image: '/image/message.png',
                duration: 2000
              });
              return;
            }
        }

        //判断添加数据项
        var templs = _this.data.templs
        for (var teml in templs) {
          console.info();
          if (!templs[teml].name) {
            wx.showToast({
              title: parseInt(teml) + 1 + "项名称必填",
              image: '/image/message.png',
              duration: 2000
            });
            return;
          }
        }

        if (status) {
            wx.showToast({
                title: '正在提交数据...',
                icon: 'loading',
                duration: 10000
            });
            app.getUserInfo(function(userInfo) {
                submitObject.avatarUrl = userInfo.avatarUrl;
                submitObject.nickName = userInfo.nickName;
                submitObject.openId = userInfo.openId;
                submitObject.templ = { 'templ': _this.data.templs, 'unit': _this.data.unit};
                submitObject.id = _this.data.id;
                console.log(submitObject);
                wx.request({
                    url: app.globalData.domain + 'api/wxapp_add',
                    data: submitObject,
                    method: 'post',
                    success: function(res) {
                        wx.hideToast();
                        wx.showToast({
                            title: '成功',
                            icon: 'success',
                            duration: 2000,
                            complete: function() {
                              wx.switchTab({
                                    url: '/pages/all/all'
                                });
                            }
                        });
                        
                        wx.request({
                            url: app.globalData.domain + 'api/wxapp_update_tpl_num/' + _id,
                            success: function(res) {
                                console.log('更新tpl total成功' + res);
                            }
                        });
                    }
                });

            });


        } else {
          wx.showToast({
            title: '数据填写不完整',
            icon: 'success',
            image: '/image/message.png',
            duration: 2000
          });
        }

    },
    onLoad: function(option) {
       
        _id = option._id;
        if(_id){
          var tm = JSON.parse(option.tm)
          this.setData({
            tplid: _id,
            imageUrl: option.id,
            theme: option.theme,
            templs: tm.templ,
            unit: tm.unit
          });
        }else{
          console.info('query');
          var _this=this;
          app.getUserInfo(function (userInfo) {
            wx.request({
              url: app.globalData.domain + 'api/wxapp_get?id=' + option.id,
              success: function (res) {
                console.info(res);
                res.data.project.map((e) => {
                  if (e.app_project.id == option.id) {
                    var tm = JSON.parse(e.app_project.templ);
                    delete e.app_project.templ;
                    _this.setData(e.app_project);
                    tm.templ.forEach(function (row) {
                      row['typevalue'] = row['typevalue'].join(",");
                    });
                    _id = e.app_tpl.id
                    _this.setData({ imageUrl: e.app_tpl.image, theme: e.app_tpl.name, templs: tm.templ, unit: tm.unit});
                    console.info(_this.data);
                  }

                });
                wx.hideNavigationBarLoading();
              }
            });
          });
        }

    }

});